package com.xizhao.boot.service;

import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.Executors;
import java.util.function.Function;

/**
 * . _________         .__   _____   __
 * ./   _____/__  _  __|__|_/ ____\_/  |_
 * .\_____  \ \ \/ \/ /|  |\   __\ \   __\
 * ./        \ \     / |  | |  |    |  |
 * /_______  /  \/\_/  |__| |__|    |__|
 * .       \/
 * <p>
 * <a href="www.google.com">google</a>
 *
 * @author li tong
 * @description:
 * @date 2022/08/18 17:59
 * @see Object
 * @since 1.0
 */
@Service
public class WebService {

	/**
	 * DeferredResult 如何实现长轮询？ https://mp.weixin.qq.com/s?__biz=Mzg2NzYyNjQzNg==&mid=2247500884&idx=1&sn=5001586690b4e71fe430205c76a64725&chksm=ceba38fff9cdb1e95c1ddbbb93d6beacd681cc428be0bbbc20f5495d752654093b0b3e270bc6&mpshare=1&scene=1&srcid=0817toSkQ6BtEG2zhVcOOkBn&sharer_sharetime=1660700620704&sharer_shareid=de38240442124bfe41fab10bbb3b36d1&version=4.0.9.70129&platform=mac#rd
	 *
	 * @return DeferredResult
	 */
	public <T, R> DeferredResult<R> deferredService(Function<T, R> function, T param, long waitTime, T defResult) {
		DeferredResult<R> deferredResult = new DeferredResult<>(waitTime, defResult);
		if (param != null)
			Executors.newSingleThreadExecutor().execute(() -> deferredResult.setResult(function.apply(param)));
		return deferredResult;
	}

	/**
	 * 测试aop
	 */
	public String printHello() {
		String ret = "hello，这里是pointcut";
		System.out.println(ret);
		return ret;
	}

}
